package vn.lumi.smarthome.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.network.NetMessage;

public class ReceiveDataThread extends Thread {

	public static final int STATE_RUNNING = 2;
	public static final int STATE_PAUSING = 1;
	public static final int STATE_END = 0;

	public static int state;
	private Handler ServiceHandler;
	private InputStream is;

	public ReceiveDataThread(InputStream is, Handler ServiceHandler) {

		this.is = is;
		this.ServiceHandler = ServiceHandler;
		state = STATE_RUNNING;
		Log.d("Receive Thread", "Created");
	}

	public static void End() {
		state = STATE_END;
		// Dong socket
		try {
			if (LumiHome.is != null)
				LumiHome.is.close();
			if (LumiHome.os != null)
				LumiHome.os.close();
			if (LumiHome.socket != null)
				LumiHome.socket.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Log.d("Receive Thread", "Service: end now");
		Thread.currentThread().interrupt();
		return;
	}

	public void Restart() {
		state = STATE_RUNNING;
		Log.d("Receive Thread", "restart");
	}

	public void Pause() {
		state = STATE_PAUSING;
		Log.d("Receive Thread", "state = pause");
	}

	@Override
	public void run() {
		super.run();
		int i = 0;
		if (state == STATE_END) {
			try {
				Thread.sleep(200);
			} catch (InterruptedException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}
			Log.d("ReceiveThread", "I'm still alive!!!");
		}
		while (state != STATE_END) {
			try {
				Thread.sleep(200);
			} catch (InterruptedException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			}

			if (state == STATE_RUNNING && LumiHome.is != null) {

				while (true) {
					try {
						if (LumiHome.is.available() != 0) {
							byte[] header = new byte[3];
							try {
								LumiHome.is.read(header, 0, 3);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								break;
							}
							int length = 16
									* NetMessage.Byte2Unsigned(header[1])
									+ NetMessage.Byte2Unsigned(header[2]);
							byte[] Msg = new byte[1 + length];
							Msg[0] = header[0];
							try {
								LumiHome.is.read(Msg, 1, length);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								break;
							}
							Log.d("Receive Thread", "New message, length = "
									+ length + ":" + Arrays.toString(Msg));
							Message Msg2Service = new Message();
							Msg2Service.what = SmartHomeService.RECEI2SER_NEW_MSG;
							Bundle b = new Bundle();
							b.putByteArray(SmartHomeService.MESSAGE, Msg);
							Msg2Service.setData(b);
							this.ServiceHandler.sendMessage(Msg2Service);
							Log.d("Receive Thread", "Sent msg to Service");
						} else {
							break;
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						break;
					}
				}
			}
		}
		Log.d("ReceiveThead", " really end!");
		return;
	}

}
